package com.acme.multithreadinglab.tracker;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class SyncTracker {
	private Map<String, Point> map;
	
	public SyncTracker(final Map<String, Point> m) {
		map = mapCopy(m);
	}

	private Map<String, Point> mapCopy(final Map<String, Point> m) {
		Map<String, Point> newMap = new HashMap<String, Point> ();
		
		for (Entry<String, Point> e: m.entrySet()) {
			newMap.put(e.getKey(), new Point(e.getValue()));
		}
		return newMap;
	}
	
	public synchronized Point getLoc(String id) {
		Point existingPoint = map.get(id); 
		return (existingPoint != null) ? new Point(existingPoint) : null;
	}
	
	public synchronized void setLoc(String id, Point point){
		map.put(id, new Point(point));
	}
	
	public synchronized Map<String, Point> getMap () {
		return mapCopy(map);
	}
}
